package com.hoshiicloud.saleorder.service.impl.sendOrder;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.saleorder.entity.DispatchBillDetail;
import com.hoshiicloud.saleorder.feign.IdGeneratorService;
import com.hoshiicloud.saleorder.mapper.sendOrder.DispatchBillDetailMapper;
import com.hoshiicloud.saleorder.service.sendOrder.DispatchBillDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @Author:campchen
 * @Date:Created in 下午 05:24 2019/8/22
 * @PACKAGENAME com.hoshiicloud.saleorder.service.impl.sendOrder
 * @Description
 */
@Service
public class DispatchBillDetailServiceImpl extends ServiceImpl<DispatchBillDetailMapper, DispatchBillDetail> implements DispatchBillDetailService {
    @Autowired
    private IdGeneratorService idGeneratorService;

    /**
     * @Author:campchen
     * @Description: 分页查询发货单明细信息
     * @Date:上午 10:45 2019/8/22
     * @Param:
     * @return:
     */
    @Override
    @Cacheable(cacheNames = "DispatchBillDetail", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "工单管理-发货单明细管理-分页查询发货单明细信息")
    public IPage<DispatchBillDetail> getPage(IPage<DispatchBillDetail> page, QueryWrapper<DispatchBillDetail> queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * @param id
     * @return com.hoshiicloud.saleorder.entity.DispatchBillDetail
     * @description: 根据id查询发货单明细信息
     */
    @Override
    @Cacheable(cacheNames = "DispatchBillDetail", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "工单管理-发货单明细管理-根据id查询发货单明细信息")
    public DispatchBillDetail getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @param entity
     * @return boolean
     * @description: 新增发货单明细信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "DispatchBillDetail", allEntries = true)
    @Logable(authName = "工单管理-发货单明细管理-新增发货单明细信息")
    public String insert(DispatchBillDetail entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        entity.setDeleted(1);
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return id;
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改经营领域附件类型信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "DispatchBillDetail", allEntries = true)
    @Logable(authName = "租户管理-经营领域附件类型管理-修改经营领域附件类型信息 ")
    public boolean updateById(DispatchBillDetail entity) {
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }
}
